package tests;
import static org.junit.Assert.*;
import model.TemplateCompiler;
import org.junit.After;
import org.junit.Test;


/**
 * This class tests the Template Compiler
 * 
 * @author Musab Masood
 * @since May 2013
 *
 * @version $Revision: 1.0 $
 */

public class TestTemplateCompiler {
	
	/**
	 * The SUT, template compiler
	 */
	private static TemplateCompiler SUT = new TemplateCompiler();
	
	/**
	 * This method is called after every test case to tear down and force
	 * garbage collection.
	 */
	@After
	public void tearDown(){
		System.gc();
	}

	/**
	 * Tests the template compiler with no tokens provided
	 */
	@Test
	public void testWithNoTokens(){
		SUT.clear();
		String body = "Hello my name is ${name}, who are you?";
		SUT.setBody(body);
		assertEquals(SUT.compile(), body);
	}
	
	/**
	 * Tests the system with a valid set of tokens and valid body
	 */
	@Test
	public void testWithValidTokensValidBody(){
		SUT.clear();
		SUT.setBody("${child} is the son of ${parent}");
		SUT.addOrReplaceToken("parent", "Masood");
		SUT.addOrReplaceToken("child", "Musab");
		String expected = "Musab is the son of Masood" ;
		assertEquals(SUT.compile(), expected);
	}
	
	/**
	 * Tests the system with an invlalid spcification of token in the body string,
	 * basically should not replace it
	 */
	@Test
	public void testWithInvalidTokenSpecificationInBody(){
		SUT.clear();
		SUT.setBody("${   child} is the son of ${parent}");
		SUT.addOrReplaceToken("parent", "Masood");
		SUT.addOrReplaceToken("child", "Musab");
		String expected = "${   child} is the son of Masood" ;
		assertEquals(SUT.compile(), expected);
	}

}
